import { getClientInfo,sendEditInfo,sendDeleteInfo } from "../../../network/client";
import { debounce,callTel, toast } from '../../../utils/Tools'
// pages/client/client-info/client-info.js
Page({
  data: {
    customer_id: null,
    isEdit:false,
    isSeleContract: false,
    sele: '1',
    clientInfo:{},
    printStatus:{
      "0":'未打印',"1":'已打印'
    },
    customer_name:'',
    telephone:'',
    address:'',
    status:{"0":'审核中','1':'已通过',"2":'未通过','3':'已完成'}
  },
  onLoad: function (options) {
    this.handleInput = debounce(this.handleInput,100)
    this._getClientInfo(options.customer_id)
    this.setData({customer_id: options.customer_id})
  },
  //      函数事件       //
  handleRadio(e){ //处理单选框选择
    this.setData({
      sele: e.detail,
      isSeleContract: false
    });
    if(this.data.sele === '1'){
      wx.navigateTo({
        url: `/pages/client/client-contract/client-contract?customer_id=${this.data.customer_id}&customer_name=${this.data.clientInfo.customer.customer_name}`
      });
    }else{
      wx.navigateTo({
        url: `/pages/client/upload-contract/upload-contract?customer_id=${this.data.customer_id}&customer_name=${this.data.clientInfo.customer.customer_name}`
      });
    }
  },
  handleInput(e){
    const {type} = e.currentTarget.dataset
    this.setData({
      [type]: e.detail.value
    })
  },
  handleCallTel(e){
    const {phone} = e.currentTarget.dataset
    if(!this.data.isEdit){
      callTel(phone)
    }
  },
  handleClientInfo(e){
    const {type} = e.currentTarget.dataset
    switch (type) {
      case 'edit':
        this.setData({isEdit:!this.data.isEdit})
        if(!this.data.isEdit){
          wx.showModal({
            title: '操作提醒',
            content: '您是否要保存客户信息',
            success: (result) => {
              if(result.confirm){
                const data={
                  customer_id:this.data.customer_id,
                  customer_name:this.data.customer_name,
                  telephone:this.data.telephone,
                  address:this.data.address
                }
                if(data.customer_name&&data.telephone&&data.address){
                  sendEditInfo(data).then(res=>{
                    if(res.code === 200){
                      toast('提交成功','success')
                    }else{
                      toast('提交失败,请重新操作')
                    }
                  })
                }
              }else{
                this.setData({isEdit:true})
              }
            }
          });
        }        
        break;
      case 'delete':
        wx.showModal({
          title: '操作提醒',
          content: '您是否要删除客户信息',
          success: (result) => {
            if(result.confirm){
              sendDeleteInfo(this.data.customer_id).then(res=>{
                if(res.code === 200){
                  toast('删除成功','success')
                  setTimeout(()=>{
                    wx.switchTab({
                      url: '/pages/client/client/client'
                    });
                  })
                }else{
                  toast('删除失败,请重新操作')
                }
              })
            }
          }
        });
        break;
    }
  },
  handleSeleContract(){ //处理点击去签合同打开选项
    this.setData({isSeleContract:true})
  },
  onClose() {
    this.setData({ isSeleContract: false });
  },
  //    网络请求      //
  async _getClientInfo(params){
    const res = await getClientInfo(params)
    if(res.code === 200){
      this.setData({
        clientInfo: res.data,
        customer_name:res.data.customer.customer_name,
        telephone:res.data.customer.telephone,
        address:res.data.customer.address
      })
    }
  }
})